﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using System.Data;
using Way.Bus.Model;
using Way.Bus.WMS.Model;

namespace Way.Bus.WMS.ST.Controllers
{
    public class ST_MoveExcelDRController : ExcelDRController
    {
        protected override string Z_FKey()
        {
            return "PID";
        }

        /// 重写转换数据方法（通过映射关系）
        protected override DataTable ConvertData(DataTable dt, ImportMapping[] mi)
        {
            //创建待导入的DataTable对象
            DataTable dtnow = new DataTable();

            foreach (ImportMapping i in mi)
            {
                if (!string.IsNullOrEmpty(i.ScName))
                {
                    dtnow.Columns.Add(i.TcName);
                }
            }
            if (dtnow.Columns.Count > 0)
            {
                //导入数据
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow drnow = dtnow.NewRow(); 
                    int rownum = dt.Rows.IndexOf(dr) + 1;
                    foreach (ImportMapping i in mi)
                    {
                        if (!string.IsNullOrEmpty(i.ScName))
                        {
                            string svalue = dr[i.ScName].ToString();
                            //判断录入数据格式是否符合规则
                            isConformWithRules(i.ScName, svalue, rownum);
                            //赋值
                            drnow[i.TcName] = svalue;
                            //默认值处理
                            if (!string.IsNullOrEmpty(i.Value))
                            {
                                drnow[i.TcName] = i.Value;
                            }
                        }
                    }
                    dtnow.Rows.Add(drnow);
                }
                return dtnow;
            }
            else
            {
                return null;
            }
        }

        //判断录入数据格式是否符合规则
        public void isConformWithRules(string sname, string svalue, int rownum)
        {
            int a = 0;
            //判断是否为空
            if ((sname.IndexOf("货物编码") > -1 
                || sname.IndexOf("货物名称") > -1
                || sname.IndexOf("源位置") > -1
                || sname.IndexOf("目标位置") > -1)
                && (svalue ==null || svalue.Equals("")) )
            {
                throw new Exception(sname + ": 不能为空，请检查Excel第" + rownum + "行");
            }
            if (sname.IndexOf("数量") > -1 && !int.TryParse(svalue, out a)) //判断是否可以转换为整型
            {
                throw new Exception(sname + ": 必须为数值型，请检查Excel第" + rownum + "行");
            }
        }

        protected override int GetChildMasterID(ExcelDRController.ImportMapping i, System.Data.DataRow dr, List<ExcelDRController.ImportMapping> im, System.Data.DataTable dt, EAP.DataAccess.Data.IDataAction action, bool isWJ)
        {
            string[] pc = Z_Z_Field();
            string pn = pc[0];
            string cn = pc[1];
            if (!string.IsNullOrEmpty(pn) && pn == "ID")
                return Convert.ToInt32(cn);
            else
                return base.GetChildMasterID(i, dr, im, dt, action, isWJ);
        }
        
       
    }
}
